package com.my.mvvmrapid.ui.main;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;

import androidx.core.content.res.ResourcesCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.ActivityMainBinding;
import com.my.mvvmrapid.ui.main.viewpager.ViewPagerFragmentStateAdapter;
import com.zhongjh.mvvmrapid.base.ui.BaseActivity;
import com.zhongjh.mvvmrapid.utils.StatusBarUtil;
import com.zhongjh.mvvmrapid.utils.ScreenUtil;

import java.util.ArrayList;

import devlight.io.library.ntb.NavigationTabBar;


/**
 * 首页
 * Created by zhongjh on 2021/3/31.
 * https://blog.mindorks.com/shared-viewmodel-in-android-shared-between-fragments 看如何处理共享数据
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {

    private ViewPagerFragmentStateAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ScreenUtil.setFullScreen(MainActivity.this, false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public boolean isStartAutoHideSoftKeyboard() {
        return false;
    }

    @Override
    public void initParam() {
    }

    @Override
    public void initData() {
        StatusBarUtil.initStatusBarHeight(binding.viewpager2);
        mAdapter = new ViewPagerFragmentStateAdapter(getSupportFragmentManager(), getLifecycle());
        binding.viewpager2.setAdapter(mAdapter);
        binding.viewpager2.setUserInputEnabled(false);
        binding.viewpager2.setOffscreenPageLimit(5);
        initTabBar();
        deleteSplash();
    }

    @Override
    public void initViewObservable() {
        binding.ntb.setOnTabBarSelectedIndexListener(new NavigationTabBar.OnTabBarSelectedIndexListener() {
            @Override
            public void onStartTabSelected(NavigationTabBar.Model model, int index) {
                binding.viewpager2.setCurrentItem(index);
            }

            @Override
            public void onEndTabSelected(NavigationTabBar.Model model, int index) {
            }
        });
        binding.viewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {


            @Override
            public void onPageSelected(int position) {
                binding.ntb.setModelIndex(position);
                super.onPageSelected(position);
            }
        });
    }

    /**
     * 初始化tabBar
     */
    private void initTabBar() {
        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(
                new NavigationTabBar.Model.Builder(
                        ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_accessibility_24, getTheme()),
                        Color.parseColor("#B0C4DE")
                ).title("Heart")
                        .badgeTitle("NTB")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_accessible_24, getTheme()),
                        Color.parseColor("#B0C4DE")
                ).title("Cup")
                        .badgeTitle("with")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_accessible_forward_24, getTheme()),
                        Color.parseColor("#B0C4DE")
                ).title("Diploma")
                        .badgeTitle("state")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_airline_seat_recline_extra_24, getTheme()),
                        Color.parseColor("#B0C4DE")
                ).title("Flag")
                        .badgeTitle("icon")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_airline_seat_recline_normal_24, getTheme()),
                        Color.parseColor("#B0C4DE")
                ).title("Medal")
                        .badgeTitle("777")
                        .build()
        );
        models.get(0).showBadge();
        models.get(0).setBadgeTitle("2");

        binding.ntb.setModels(models);
    }

    /**
     * 删除初始界面
     */
    private void deleteSplash() {
        binding.imgSplash.animate().alpha(0).setDuration(500).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 删除控件
                ((ViewGroup) (binding.imgSplash.getParent())).removeView(binding.imgSplash);
            }
        });
    }

    /**
     * 监听返回--是否退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean flag;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 是否退出应用
            boolean d = moveTaskToBack(false);
            if (!d) {
                moveTaskToBack(true);
            }
            return true;
        } else {
            flag = super.onKeyDown(keyCode, event);
        }
        return flag;
    }

}
